var timeoutId = false;
var g_nullDate = '1970-01-01';
var months=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
// utility function
function padZero(num) {
  return ((num <= 9) ? ("0" + num) : num);
}

function formateHideValueToSystemDBDate(y,m,d)
{
  var sysDBFormat = DB_DATETIME_FORMAT.toLowerCase();
  if (sysDBFormat == ''){
	return '';
  }	  
  var datetime_arr=sysDBFormat.split(" ");
  var dateStr = datetime_arr[0];
  var timeStr = datetime_arr[1];
  var delim = '';
  var Digital = true;
  var ShortYear=false;
  var iDatePos = -1; 
	switch( dateStr){
	  case 'dd-mmm-yyyy':
		delim =  '-';
		break;
	  case 'dd/mm/yyyy':
	  case 'dd-mm-yyyy':
		delim = dateStr.substring(2,3);
		iDatePos = 0;
		break;
	  case 'mm/dd/yyyy':
	  case 'mm-dd-yyyy':
		delim = dateStr.substring(2,3);
		iDatePos = 1;
		break;
	  case 'yyyy/mm/dd':
	  case 'yyyy-mm-dd':
		delim = dateStr.substring(4,5);
		iDatePos = 2;
		break;
	  case 'yyyy/dd/mm':
	  case 'yyyy-dd-mm':
		delim = dateStr.substring(4,5);
		iDatePos = 3;
		break;
	}
	var M=Digital?m<10?"0"+m:m:gMonths[m-1];
	var D=Digital&&d<10?"0"+d:d;
	var sy=y%100;
	var sSplit = delim;
	sy=sy<10?"0"+sy:sy;
	switch (iDatePos) {
		case 0: return D+sSplit+M+sSplit+(ShortYear?sy:y);
		case 1: return M+sSplit+D+sSplit+(ShortYear?sy:y);
		case 2: return (ShortYear?sy:y)+sSplit+M+sSplit+D;
		case 3: return (ShortYear?sy:y)+sSplit+D+sSplit+M;
	}
}

function isdig(s)
{
	var regu = "^([1-9]*[0-9]*)$"
	var re = new RegExp(regu);
	if (s.search(re) != -1){
		return true;
	}else{
		return false;
	}
}

function fcheck_date(elm, format,needReload,onChange) {
	var errorFlag = false;
	var delim;
	var matched;
	var valueStr = elm.value; 

	var frm = elm.form ;
    var pos = (elm.name).indexOf("_");
    var targetSubName = (elm.name).substr(pos+1);
	var hDate =  eval('frm.'+targetSubName);
	var hourObj = eval('frm.hour_dummy_'+targetSubName);
	var minuteObj = eval('frm.min_dummy_'+targetSubName);
	if (valueStr.length == 0) {
		hDate.value = '';
		errorFlag = false;
		return errorFlag;
	} else {
		var datetime_arr=valueStr.split(" ");
		
		var dateStr = datetime_arr[0];
		var timeStr = datetime_arr[1];


		var dateRegexp;
		var dayVal, monthVal, yearVal;
		

		switch( format){
		  case 'dd-mmm-yyyy':
			delim =  '-';
			dateRegexp = new RegExp("^(\\d{2})[-](\\S{3})[-](\\d{4})$");
			matched = dateRegexp.exec(dateStr);
			if (matched != null) {
				  dayVal =  parseInt(dateStr.substring(0, 2), 10);
				  stmp = dateStr.substring(3, 6);
				  for (var i=0;i<this.months.length;i++){
					if (stmp.toLowerCase()==this.months[i].substr(0,3).toLowerCase()){
					  monthVal = i+1;
					  break;
					}
				  }
				  yearVal = parseInt(dateStr.substring(7), 10);
			} else {
			  errorFlag = true;
			  //return (!errorFlag);
			}
			break;
		  case 'dd/mm/yyyy':
		  case 'dd-mm-yyyy':
			delim = format.substring(2,3);
			dateRegexp = new RegExp("^(\\d{2})["+delim+"](\\d{2})["+delim+"](\\d{4})$");
			matched = dateRegexp.exec(dateStr);
			if (matched != null) {
				  dayVal = parseInt(dateStr.substring(0,2), 10);
				  monthVal = parseInt(dateStr.substring(3,5), 10);
				  yearVal = parseInt(dateStr.substring(6), 10);
			} else {
			  errorFlag = true;
			  //return (!errorFlag);
			}
			break;
		  case 'mm/dd/yyyy':
		  case 'mm-dd-yyyy':
			delim = format.substring(2,3);
			dateRegexp = new RegExp("^(\\d{2})["+delim+"](\\d{2})["+delim+"](\\d{4})$");
			matched = dateRegexp.exec(dateStr);
			if (matched != null) {
				  dayVal = parseInt(dateStr.substring(3,5), 10);
				  monthVal = parseInt(dateStr.substring(0,2), 10);
				  yearVal = parseInt(dateStr.substring(6), 10);
			} else {
			  errorFlag = true;
			  //return (!errorFlag);
			}
			break;
		  case 'yyyy/mm/dd':
		  case 'yyyy-mm-dd':
			delim = format.substring(4,5);

			dateRegexp = new RegExp("^(\\d{4})["+delim+"](\\d{2})["+delim+"](\\d{2})$");
			matched = dateRegexp.exec(dateStr);
			if (matched != null) {
				  dayVal = parseInt(dateStr.substring(8), 10);
				  monthVal = parseInt(dateStr.substring(5,7), 10);
				  yearVal = parseInt(dateStr.substring(0,5), 10);
			} else {
			  errorFlag = true;
			  //return (!errorFlag);
			}
			break;
		  case 'yyyy/dd/mm':
		  case 'yyyy-dd-mm':
			delim = format.substring(4,5);
			dateRegexp = new RegExp("^(\\d{4})["+delim+"](\\d{2})["+delim+"](\\d{2})$");
			matched = dateRegexp.exec(dateStr);
			if (matched != null) {
				  dayVal = parseInt(dateStr.substring(5,7), 10);
				  monthVal = parseInt(dateStr.substring(8), 10);
				  yearVal = parseInt(dateStr.substring(0,5), 10);
			} else {
			  errorFlag = true;
			  //return (!errorFlag);
			}
			break;
		}

		var tmpDate = new Date(yearVal, (monthVal-1), dayVal);
		
		if (tmpDate.getFullYear() == yearVal
			&& tmpDate.getMonth() == (monthVal-1)
			&& tmpDate.getDate() == dayVal) {
			errorFlag = false;
		} else {
			errorFlag = true;
		}

		//Time
		if(timeStr){
		  var time_arr=timeStr.split(":");
		  hour=time_arr[0];
		  min=time_arr[1];
		  sec=time_arr[2];
		  if (!isdig(hour) || !isdig(min) || !isdig(sec) || hour<0 || hour>23 || min<0 || min>59 || sec<0 || sec>59)
			errorFlag = true;
		}
	}


	if (errorFlag) {
		alert("Invalid Date Format, should be (" + format + ")");
//		elm.select();
		elm.focus();
	} else {
//		errorFlag = this.formatActualValue(elm, format, delim);
		if(hDate){
//		  hDate.value = yearVal+'-'+monthVal+'-'+dayVal;
		  var origValue = hDate.value;
		  hDate.value = formateHideValueToSystemDBDate(yearVal,monthVal,dayVal);
	      if (hourObj) {
	        hDate.value = hDate.value + ' ' + hourObj.value + ':' + minuteObj.value + ':00';
	      } else {
	        hDate.value = hDate.value + ' 00:00:00';
	      }
	      if( origValue != hDate.value && hDate.onchange ){
	      	hDate.onchange();
	      }
		  if (onChange){
			onchange();
		  }
		  if (needReload){
			OpRefresh();
		  }
		}
	} 
	return(!errorFlag);
}

function formatActualValue (elm, format, delim) {
      var errorFlag = false;
	  var day,month,year;
	  var frm;	
	  var dateFormat,dateDelim,pos
	  var hDate_null,hDate;
      frm = elm.form;
      if (format!=null) {
        dateFormat = format;
      }

      if (delim!=null) {
        dateDelim = delim;
      }

	  switch (dateFormat){
	   case 'dd-mmm-yyyy':
	     g_nullDate = '01-01-1970';
	     break;
	   case 'dd/mm/yyyy':
	   case 'dd-mm-yyyy':
		  if(dateDelim=='/')
		 	g_nullDate = '01/01/1970';
		  else
			g_nullDate = '01-01-1970';
	     break;
	   case 'mm/dd/yyyy':
  	   case 'mm-dd-yyyy':
		  if(dateDelim=='/')
		  	  g_nullDate = '01/01/1970';
		  else
			  g_nullDate = '01-01-1970';
	     break;
	   case 'yyyy-mm-dd':
	   case 'yyyy/mm/dd':
		  if(dateDelim=='/')
			g_nullDate = '1970/01/01';
		  else
			g_nullDate = '1970-01-01';
	     break;
	   case 'yyyy/dd/mm':
       case 'yyyy-dd-mm':
		  if(dateDelim=='/')
			g_nullDate = '1970/01/01';
		  else
			g_nullDate = '1970-01-01';
	  }

	  if(dateDelim=='/')
		hDate_null = '1970/01/01';
	  else
		hDate_null = '1970-01-01';

      pos = (elm.name).indexOf("_");
      var targetSubName = (elm.name).substr(pos+1);
      pos = (targetSubName).indexOf("_");
      var targetName = (targetSubName).substr(pos+1);

      var hour = eval('frm.hour_'+targetSubName);
      var minute = eval('frm.min_'+targetSubName);
      var hiddenVal = eval('frm.'+targetName);
      var dateVal = eval('frm.dummy_'+targetName);

      var sHour, sMinute, sDate;
      if (hour) {
        sHour = hour.value;
      } else {
        sHour = '00';
      }

      if (minute) {
        sMinute = minute.value;
      } else {
        sMinute = '00';
      }
      if (dateVal && dateVal.value!='') {
    	    var atmp = dateVal.value.split(dateDelim);
      		switch (dateFormat){
      		 case 'dd-mmm-yyyy':
      		   for (var i=0;i<months.length;i++){
      		     if (atmp[1].toLowerCase()==months[i].substr(0,3).toLowerCase()){
      		       month = oMonth = i;
      			   break;
      		     }
      		   }
      		   day = parseInt(atmp[0],10);
      		   year = parseInt(atmp[2],10);
        	   sDate = padZero(day) + dateDelim +padZero((month+1)) + dateDelim + year;
			   hDate = year + dateDelim + padZero((month+1)) + dateDelim + padZero(day);
			   hDate = formateHideValueToSystemDBDate(year,month+1,day);
      		   break;
      		 case 'dd/mm/yyyy':
      		 case 'dd-mm-yyyy':
      		   month = oMonth = parseInt(atmp[1]-1,10);
      		   day = parseInt(atmp[0],10);
      		   year = parseInt(atmp[2],10);
        	   sDate =padZero(day) + dateDelim + padZero((month+1)) + dateDelim  +  year;
			   hDate = formateHideValueToSystemDBDate(year,month+1,day);
      		   break;
      		 case 'mm/dd/yyyy':
      		 case 'mm-dd-yyyy':

      		   month = oMonth = parseInt(atmp[0]-1,10);
      		   day = parseInt(atmp[1],10);
      		   year = parseInt(atmp[2],10);
			   sDate = padZero((month+1)) + dateDelim +  padZero(day) + dateDelim  + year;
			   hDate = formateHideValueToSystemDBDate(year,month+1,day);
      		   break;
      		 case 'yyyy-mm-dd':
      		 case 'yyyy/mm/dd':
      		   month = oMonth = parseInt(atmp[1]-1,10);
      		   day = parseInt(atmp[2],10);
      		   year = parseInt(atmp[0],10);
			   sDate = year + dateDelim + padZero((month+1)) + dateDelim + padZero(day);
			   hDate = formateHideValueToSystemDBDate(year,month+1,day);
      		   break;
      		 case 'yyyy/dd/mm':
             case 'yyyy-dd-mm':
      		   month = oMonth = parseInt(atmp[2]-1,10);
      		   day = parseInt(atmp[1],10);
      		   year = parseInt(atmp[0],10);
			   sDate = year + dateDelim + padZero(day) +  dateDelim + padZero((month+1)) ;
			   hDate = formateHideValueToSystemDBDate(year,month+1,day);
      		}			
      } else {
        g_nullDate='';
        hDate_null='';
        sDate = g_nullDate;
		hDate = hDate_null;
      }
      if (sDate!='') {
//  	dateVal.value = sDate + ' ' + sHour + ':' + sMinute + ':00';	
  	    dateVal.value = sDate;	
        hiddenVal.value = hDate + ' ' + sHour + ':' + sMinute + ':00';
      } else {
        dateVal.value = '';
        hiddenVal.value = '';
      }
      return (!errorFlag);
  }


var outStr = "";
outStr +='<!--  PopCalendar Start (name and id must match) -->\n';
outStr +='<iframe name=\"gToday:normal\" id=\"gToday:normal\" src=\"' + CONTEXT_PATH + '/js/PopupCalendar.html\" scrolling=\"no\" frameborder=\"0\" style=\"visibility:hidden;position:absolute;z-index:65535\">';
outStr +='</iframe>';
outStr +='<!--  PopCalendar End (DO NOT REPEAT this part! One is enough.) -->';
document.write(outStr);

var arrRange=depRange=[];
